package com.yupi.springbootinit.service.workflow;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yupi.springbootinit.mapper.DepartmentMapper;
import com.yupi.springbootinit.model.entity.Department;
import com.yupi.springbootinit.model.entity.WorkflowTask;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.yupi.springbootinit.service.workflow.WorkflowTaskConstants.*;

/**
 * 工作流任务工具类
 */
@Component
public class WorkflowTaskUtils {

    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 根据部门代码获取部门信息
     */
    public Department getDepartmentByCode(String deptCode) {
        LambdaQueryWrapper<Department> qw = new LambdaQueryWrapper<>();
        qw.eq(Department::getDepartmentCode, deptCode)
          .eq(Department::getIsDelete, 0)
          .last("limit 1");
        return departmentMapper.selectOne(qw);
    }

    /**
     * 获取状态显示名称
     */
    public String getStatusDisplayName(String status) {
        if (StringUtils.isBlank(status)) {
            return "";
        }
        return switch (status) {
            case STATUS_PENDING -> "待处理";
            case STATUS_IN_PROGRESS -> "进行中";
            case STATUS_DONE -> "已完成";
            default -> status;
        };
    }

    /**
     * 获取优先级显示名称
     */
    public String getPriorityDisplayName(String priority) {
        if (StringUtils.isBlank(priority)) {
            return "";
        }
        return switch (priority) {
            case PRIORITY_NORMAL -> "普通";
            case PRIORITY_HIGH -> "高";
            case PRIORITY_URGENT -> "紧急";
            default -> priority;
        };
    }

    /**
     * 判断任务是否逾期
     */
    public boolean isTaskOverdue(WorkflowTask task) {
        if (task.getExpectedCompleteTime() == null) {
            return false;
        }
        return LocalDateTime.now().isAfter(task.getExpectedCompleteTime()) 
               && !STATUS_DONE.equals(task.getTaskStatus());
    }
}